﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CocaCola_Inventory_System.Forms
{
    public partial class userMan : Form
    {

        public userMan()
        {
            InitializeComponent();
            lst_columns.SelectedIndex = 0;
        }

        private void btn_search_Click(object sender, EventArgs e)
        {
            btn_search.Enabled = false;
            btn_save.Enabled = false;
            btn_search.Text = "Searching...";
            this.Text="User Manager | Searching for "+txt_search.Text+" at "+lst_columns.Items[lst_columns.SelectedIndex].ToString()+" Field(s)";
            if(chk_match.Checked)
                this.Text = this.Text + " (Match Case)";
            string group = "";

            DataTable temp = Data_Access.searchUsers.search(((CocaCola_Inventory_System.Forms.Container)this.MdiParent).con, txt_search.Text, lst_columns.SelectedIndex, chk_match.Checked, group);

            grd_results.DataSource = temp;
            grd_results.Columns[0].Visible = false;
            grd_results.Refresh();

            btn_search.Enabled = true;
            btn_save.Enabled = true;
            btn_search.Text = "Search";
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            btn_search.Enabled = false;
            btn_save.Enabled = false;
            btn_save.Text = "Saving...";

            DataTable temp = grd_results.DataSource as DataTable;
            if (Data_Access.searchUsers.update(temp))
            {
                if (MessageBox.Show("Changes Saved, Would you like to close this window?", "Success", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                    this.Close();
            }
            else
            {
                MessageBox.Show("Saving changes failed!!!", "Failure", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }

            btn_search.Enabled = true;
            btn_save.Enabled = true;
            btn_save.Text = "Save Changes";
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_insert_Click(object sender, EventArgs e)
        {
            RegisterUser t = new RegisterUser();
            t.MdiParent = this.MdiParent;
            t.Show();
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            if (grd_results.SelectedRows.Count > 0)
            {
                if (grd_results.SelectedRows.Count > 1)
                {
                    if (MessageBox.Show("Are you sure you want to delete the selected rows?", "Delete Rows", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                    {
                        int ldeleted = -1;
                        Stack<DataGridViewRow> dels = new Stack<DataGridViewRow>(grd_results.SelectedRows.Count);
                        foreach (DataGridViewRow x in grd_results.SelectedRows)
                        {
                            if (x.Index != ldeleted)
                            {
                                dels.Push(x);
                                ldeleted = x.Index;
                            }
                        }

                        while (dels.Count > 0)
                        {
                            grd_results.Rows.Remove(dels.Pop());
                        }
                    }
                }
                else
                {
                    string nm = (string)grd_results[1, grd_results.SelectedRows[0].Index].Value;
                    if (MessageBox.Show("Are you sure you want to delete \"" + nm + "\"?", "Delete " + nm, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                        grd_results.Rows.Remove(grd_results.SelectedRows[0]);
                }
            }
            else
            {
                MessageBox.Show("You have to select a row first", "Delete Entry", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

    }
}